﻿using System.Data;
using ContractManagementSystem.EntityObjects;
using ContractManagementSystem.BusinessLogicLayer;

namespace ContractManagementSystem.DataAccessLayer
{
    public static class AccountDao
    { 
        // Get detail information of account
        public static AccountEntity ViewAccountDetail(string userName)
        {
            var dataTable = CommonDao.GetDataTable("spGetAccountList @UserName = '" + userName + "'");
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                var accountEntity = new AccountEntity
                {
                    UserName = CommonLogic.ConvertToString(dataTable.Rows[0]["Username"]),
                    Password = CommonLogic.ConvertToString(dataTable.Rows[0]["Password"]),
                    RoleId = CommonLogic.ConvertToNumber(
                        CommonLogic.ConvertToString(dataTable.Rows[0]["RoleId"])),
                    Description = CommonLogic.ConvertToString(dataTable.Rows[0]["Description"]),
                    Owner = CommonLogic.ConvertToString(dataTable.Rows[0]["Owner"]),
                    Status = CommonLogic.ConvertToNumber(
                            CommonLogic.ConvertToString(dataTable.Rows[0]["Status"])), 
                    RoleName = CommonLogic.ConvertToString(dataTable.Rows[0]["RoleName"])
                };

                return accountEntity;
            }

            return null; // Default return
        }

        // Get auto-generated user name
        public static string AutoUserName(string value)
        {
            var scalarSql = "spGenerateAutoUserName @UserName = '" + value + "'";

            var result = CommonDao.ExecuteSqlScalar(scalarSql);

            return result;
        }

        // Insert Account
        public static string AddNewAccount(AccountEntity accountEntity)
        {
            // If the account is already existed, exit without inserting
            if (ViewAccountDetail(accountEntity.UserName) != null )
            {
                return "The account has already existed. Please input another.";
            }
            var sqlInsert = " [spOperateAccount] @Username = '" + accountEntity.UserName + "', @roleId = '" + CommonLogic.ConvertToString(accountEntity.RoleId) + "',"
                            + "@password = '" + accountEntity.Password + "', @description = N'" + accountEntity.Description + "', @operationFlag = 'i', @status = '" +
                            CommonLogic.ConvertToString(accountEntity.Status) + "', @actorType = '" + accountEntity.ActorType + "'";

            return CommonDao.ExecuteSqlCommand(sqlInsert);
        }
		
        // Request Authentication whenever user logs on to the system
        public static AccountEntity Authenticate(string userName, string password)
        {
            var dataTable = CommonDao.GetDataTable("spGetAccountList @UserName = '" + userName + "', @Password='" + password + "'");

            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                var accountEntity = new AccountEntity
                                        {
                                            UserName = CommonLogic.ConvertToString(dataTable.Rows[0]["Username"]),
                                            Password = CommonLogic.ConvertToString(dataTable.Rows[0]["Password"]),
                                            RoleId = CommonLogic.ConvertToNumber(
                                                CommonLogic.ConvertToString(dataTable.Rows[0]["RoleId"]))
                                        };

                return accountEntity;
            }

            return null; // Default return
        }
    }
}
